package model.requirement3;


import model.DatabaseConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SpecialPizzaDAO {

    // 添加特色披萨
    public void addPizza(int storeID, String name, String toppings, double price) throws SQLException {
        String query = "INSERT INTO SpecialPizzas (StoreID, Name, Toppings, Price) VALUES (?, ?, ?, ?)";
        try (Connection connection = DatabaseConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setInt(1, storeID);
            statement.setString(2, name);
            statement.setString(3, toppings);
            statement.setDouble(4, price);
            statement.executeUpdate();
        }
    }

    // 查询某个门店的所有披萨
    public List<SpecialPizza> getPizzasByStoreID(int storeID) throws SQLException {
        List<SpecialPizza> pizzas = new ArrayList<>();
        String query = "SELECT * FROM SpecialPizzas WHERE StoreID = ?";
        try (Connection connection = DatabaseConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setInt(1, storeID);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                pizzas.add(new SpecialPizza(
                        resultSet.getInt("PizzaID"),
                        resultSet.getInt("StoreID"),
                        resultSet.getString("Name"),
                        resultSet.getString("Toppings"),
                        resultSet.getDouble("Price")
                ));
            }
        }
        return pizzas;
    }

    // 删除特色披萨
    public void deletePizza(int pizzaID) throws SQLException {
        String query = "DELETE FROM SpecialPizzas WHERE PizzaID = ?";
        try (Connection connection = DatabaseConnection.getConnection();
             PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setInt(1, pizzaID);
            statement.executeUpdate();
        }
    }
}
